<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtheadercode.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("platinumgrid/jtplatinumgrid.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class admin_active_monitoring extends Page
{
       public $JTHeaderGrey = null;
       public $btnSearch = null;
       public $edtSearch = null;
       public $Image4 = null;
       public $JTSiteTheme1 = null;
       public $JTActiveMonitoring = null;
       function admin_active_monitoringJSLoad($sender, $params)
       {

       ?>
       document.getElementById('JTActiveMonitoring_colitem_hdr_0').width = '170px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_1').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_2').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_3').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_4').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_5').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_hdr_6').width = '90px';

       document.getElementById('JTActiveMonitoring_colitem_body_0').width = '170px';
       document.getElementById('JTActiveMonitoring_colitem_body_1').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_body_2').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_body_3').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_body_4').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_body_5').width = '90px';
       document.getElementById('JTActiveMonitoring_colitem_body_6').width = '90px';
       <?php

       }

       function edtSearchJSClick($sender, $params)
       {

       ?>
       document.getElementById('edSearch').value = "";

       <?php

       }

       function JTActiveMonitoringShow($sender, $params)
       {
       $query_company = "SELECT company_id, company_name, approval FROM company_tbl WHERE company_name LIKE '%".$this->edtSearch->Text."%' ORDER BY approval DESC, company_id ASC";
       $result_company = mysql_query($query_company) or die("SELECT COMPANIES ".mysql_error());

       $array_contents = array();
       while($row_company = mysql_fetch_array($result_company))
       {
        $company_name = $row_company['company_name'];
        $company_id = $row_company['company_id'];
        $flag_approve = $row_company['approval'];

        //veh_workorder_tbl
        //veh_fuel_tbl
        //veh_expense_tbl
        //veh_incident_tbl

        //route_tbl
        //vehicle_tbl
        $count_work = 0;
        $count_fuel = 0;
        $count_expense = 0;
        $count_incident = 0;

        $count_route = 0;
        $count_veh = 0;

        //COUNT FOR WORK SUMMARY AND EXPENSE
        $query_veh = "SELECT vehicle_id FROM vehicle_tbl WHERE company_id='".$company_id."' ORDER BY vehicle_id ASC";
		    $result_veh = mysql_query($query_veh) or die("SELECT VEHICLES ".mysql_error());
        while($row_veh = mysql_fetch_array($result_veh))
        {
         $query_count_work = "SELECT count(workorder_id) AS cnt_work FROM veh_workorder_tbl WHERE vehicle_id='".$row_veh['vehicle_id']."'";
         $result_count_work = mysql_query($query_count_work) or die("COUNT WORKORDER ".mysql_error());
         $row_count_work = mysql_fetch_array($result_count_work);
         $count_work += $row_count_work['cnt_work'];

         $query_count_exp = "SELECT count(payment_id) AS cnt_exp FROM veh_expense_tbl WHERE vehicle_id='".$row_veh['vehicle_id']."'";
         $result_count_exp = mysql_query($query_count_exp) or die("COUNT EXPENSE ".mysql_error());
         $row_count_exp = mysql_fetch_array($result_count_exp);
         $count_expense += $row_count_exp['cnt_exp'];
        }

        //COUNT VEHICLES
        $query_count_veh = "SELECT count(vehicle_id) AS cnt_veh FROM vehicle_tbl WHERE company_id='".$company_id."'";
        $result_count_veh = mysql_query($query_count_veh) or die("COUNT VEHICLE ".mysql_error());
        $row_count_veh = mysql_fetch_array($result_count_veh);
        $count_veh = $row_count_veh['cnt_veh'];

        //COUNT FOR FUEL
        $query_count_fuel = "SELECT count(fuel_id) AS cnt_fuel FROM veh_fuel_tbl WHERE company_id='".$company_id."'";
        $result_count_fuel = mysql_query($query_count_fuel) or die("COUNT FUEL ".mysql_error());
        $row_count_fuel = mysql_fetch_array($result_count_fuel);
        $count_fuel = $row_count_fuel['cnt_fuel'];

        //COUNT FOR INCIDENT
        $query_count_inc = "SELECT count(incident_id) AS cnt_inc  FROM veh_incident_tbl WHERE company_id='".$company_id."'";
        $result_count_inc = mysql_query($query_count_inc) or die("COUNT INCIDENT ".mysql_error());
        $row_count_inc = mysql_fetch_array($result_count_inc);
        $count_incident = $row_count_inc['cnt_inc'];

        //COUNT FOR ROUTE
        $query_count_route = "SELECT count(route_id) AS cnt_route  FROM route_tbl WHERE company_id='".$company_id."'";
        $result_count_route = mysql_query($query_count_route) or die("COUNT ROUTE ".mysql_error());
        $row_count_route = mysql_fetch_array($result_count_route);
        $count_route = $row_count_route['cnt_route'];

        $company_grey = "<a href=admin_view_company.php?company_id=".$company_id." title='View Company' rel='gb_page_center[409, 310]'>".$company_name."</a>";

        if($flag_approve=='0')
         {
         $activated = "<img src='image/x.png' style='height:15px;width:15px'/>&nbsp;";
         }
         if($flag_approve=='1')
         {
         $activated = "<img src='image/check.png' style='height:15px;width:15px'/>&nbsp;";
         }

        $column1 = $activated.$company_grey;//$column1 = $company_name;
        $column2 = $count_veh;
        $column3 = $count_work;
        $column4 = $count_fuel;
        $column5 = $count_expense;
        $column6 = $count_incident;
        $column7 = $count_route;

        $array_contents[] = array(
        'Company'=>$column1,
        'Vehicle'=>$column2,
        'Work<br />Summary'=>$column3,
        'Fuel'=>$column4,
        'Expense'=>$column5,
        'Incident'=>$column6,
        'Route'=>$column7
        );
       }
       $this->JTActiveMonitoring->setCellData($array_contents);
       }

}

global $application;

global $admin_active_monitoring;

//Creates the form
$admin_active_monitoring=new admin_active_monitoring($application);

//Read from resource file
$admin_active_monitoring->loadResource(__FILE__);

//Shows the form
$admin_active_monitoring->show();

?>